package cn.hzxs.zjn.controller;

import cn.hzxs.zjn.pojo.Account;
import cn.hzxs.zjn.pojo.Folder;
import cn.hzxs.zjn.pojo.Note;
import cn.hzxs.zjn.service.FoldersAndNotesService;
import cn.hzxs.zjn.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Zjn ; Bring it on !!!
 * @Date: 2021/4/23 0023 7:51
 * Note:
 */
@RestController
public class FoldersAndNotesController {
    @Autowired
    FoldersAndNotesService foldersAndNotesService;

    @PostMapping("/mobile/userLogin")
    public JsonResult userLogin(@RequestBody Account account) {
        String passWordBy = foldersAndNotesService.findPassWordByuUserName(account.getUSERNAME());
        if (account.getPASSWORD().equals(passWordBy)) {
            Map infoByUserName = foldersAndNotesService.findInfoByUserName(account.getUSERNAME());
            String noteCount = foldersAndNotesService.noteCount(foldersAndNotesService.selectUserId(account.getUSERNAME()));
            infoByUserName.put("dayCount","1");
            infoByUserName.put("noteCount",noteCount);
            return new JsonResult(infoByUserName);
        }
        return new JsonResult("fail");
    }


    @GetMapping("/getNotes")
    public JsonResult getNotes(@RequestParam("userId") BigInteger userId){
        HashMap<Object, Object> hashMap = new HashMap<>();
        List<Map> notes = foldersAndNotesService.getNotes(userId);
        for (int i = 0; i < notes.size(); i++) {
            hashMap.put(i,notes.get(i));
        }
        return new JsonResult(hashMap);
    }

    @GetMapping("/getFolders")
    public JsonResult getFolders(@RequestParam("userId") BigInteger USERID){
        HashMap<Object, Object> hashMap = new HashMap<>();
        List<Map> folders = foldersAndNotesService.getFolders(USERID);
        for (int i = 0; i < folders.size(); i++) {
            hashMap.put(i,folders.get(i));
        }
        return new JsonResult(hashMap);
    }

    @GetMapping("/getFolderNotes")
    public JsonResult getFolderNotes(@RequestParam("folderId") BigInteger FOLDERID
                                ,@RequestParam("userId") BigInteger USERID) {
        HashMap<Object, Object> hashMap = new HashMap<>();
        List<Map> folderNotes = foldersAndNotesService.getFolderNotes(FOLDERID, USERID);
        for (int i = 0; i < folderNotes.size(); i++) {
            hashMap.put(i,folderNotes.get(i));
        }
        return new JsonResult(hashMap);
    }

    @PostMapping("/addNote")
    public JsonResult addNote(@RequestBody Note note){
        int note_id = foldersAndNotesService.addNote(note);
        HashMap<Object, Object> hashMap = new HashMap<>();
        hashMap.put("noteId",note_id);
        return new JsonResult(hashMap);
    }

    @PostMapping("/addFolder")
    public JsonResult addFolder(@RequestBody Folder folder){
        int folder_id = foldersAndNotesService.addFolder(folder);
        HashMap<Object, Object> hashMap = new HashMap<>();
        hashMap.put("folderId",folder_id);
        return new JsonResult(hashMap);
    }
}
